const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
const port = 2989;

app.use(cors()); // 处理跨域
app.use(express.json()); // 解析JSON请求体

// DeepSeek API配置
const DEEPSEEK_API_URL = 'https://api.deepseek.com/v1/chat/completions'; // 替换为实际API地址
const API_KEY = 'sk-d40af8f1b6f54df89b9209140694333e'; // 从环境变量读取更安全

app.post('/api/deepseek', async (req, res) => {
    try {
        const { prompt } = req.body;
        
        const response = await axios.post(DEEPSEEK_API_URL, {
            messages: [{ role: 'user', content: prompt }],
            model: 'deepseek-chat' // 根据文档调整模型名
        }, {
            headers: {
                'Authorization': `Bearer ${API_KEY}`,
                'Content-Type': 'application/json'
            }
        });

        // 提取API返回的有效数据（根据实际响应结构调整）
        const result = response.data.choices[0].message.content;
        res.json({ response: result });
    } catch (error) {
        console.error('API调用失败:', error);
        res.status(500).json({ error: '服务暂不可用' });
    }
});

app.listen(port, () => {
    console.log(`后端服务运行在 http://localhost:${port}`);
});